/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package facade;

import entity.Inversor;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author ari
 */
@Stateless
public class InversorFacade extends AbstractFacade<Inversor> implements InversorFacadeLocal {
    @PersistenceContext(unitName = "BolsaValorWeb-ejbPU")
    private EntityManager em;

    private static final Logger logger = Logger.getLogger("BolsaValor");
    
    @Override
    protected EntityManager getEntityManager() {
        return em;
    }

    public InversorFacade() {
        super(Inversor.class);
    }

    @Override
    public boolean isUsuario(String login, String clave) {
        List<Inversor> invs = em.createQuery("SELECT i FROM Inversor i WHERE i.login = :login AND i.clave = :clave", Inversor.class)
                .setParameter("login", login)
                .setParameter("clave", clave)
                .getResultList();
        
        if(invs.isEmpty())
            return false;
        
        logger.log(Level.INFO, "Usuario logeado: {0} ", login);

        return true;
    }

    @Override
    public boolean createUsuario(String name, String pass) {
        List<Inversor> inv = em.createQuery("SELECT i FROM Inversor i WHERE i.login = :name")
                .setParameter("name", name)
                .getResultList();
        
        if(inv.isEmpty()) {
            Inversor i = new Inversor();
            i.setLogin(name);
            i.setClave(pass);
            create(i);
            
            return true;
        }
        
        return false;
    }
    
    
}
